package com.Ling.Thread.sync;

import java.util.concurrent.locks.ReentrantLock;

/**
 * File Name: LockDemo
 * Date: 2020-06-28 10:12
 * Author: liangling
 * Description
 */
public class LockDemo {



    public static void main(String[] args) {
        //实例化锁对象
        ReentrantLock lock = new ReentrantLock();

        Runnable r = ()->{
            while (TicketCenter.ticketCount>0){
                //对临界资源进行上锁
                lock.lock();
                if (TicketCenter.ticketCount<=0){
                   return;
                }
                System.out.println(Thread.currentThread().getName()+"卖出一张票，剩余"+ --TicketCenter.ticketCount+" 张");
                    //对临界资源进行解锁
                lock.unlock();

            }
        };
        Thread t1 = new Thread(r,"thread-1");
        Thread t2 = new Thread(r,"thread-2");
        Thread t3 = new Thread(r,"thread-3");
        Thread t4 = new Thread(r,"thread-4");

        t1.start();
        t2.start();
        t3.start();
        t4.start();

    }


}
